{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ef79dc4bd46859ab",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# How to generate image using Towhee\n",
    "\n",
    "\n",
    "## Introduction\n",
    "\n",
    "We will use a Towhee operator [text2image/stable-diffusion](https://towhee.io/text2image/stable-diffusion) to generate image given text.\n",
    "\n",
    "### What is Image Generation?\n",
    "\n",
    "The field of generative image synthesis involves creating images using algorithms and models, often based on machine learning techniques. It combines computer vision, machine learning, and computational creativity to __generate new and visually appealing images that can be used in various applications.__\n",
    "\n",
    " There are also a large number of models that can be used during image generation:__GANs,VAEs,diffusion models__ and other efficient ones.They have led to impressive results in image synthesis, including generating photorealistic images, creating novel artwork, and even transferring styles between images.\n",
    " \n",
    "![image](./img.png)\n",
    "\n",
    "This little cute corgi picture is actually a product of image generation. \n",
    "\n",
    "### What is Towhee?\n",
    "\n",
    "Towhee is an open-source embedding framework that includes adequate data processing algorithms and neural network models. __With Towhee, you can easily process unstructured data (such as pictures, videos, audio, long texts and other unstructured data) and complete the conversion of raw data to vectors__.It is also an open algorithm and model exchange community where developers from all over the world can freely share models and pipelines to promote technology and efficiency.\n",
    "\n",
    "You can get to more useful information about this magic framework by clicking here [towhee](https://towhee.io/)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b6fd386",
   "metadata": {},
   "source": [
    "## Preparation\n",
    "\n",
    "You are able to have more knowledge about how to generate images with towhee through the following materials.\n",
    "\n",
    "### Install Packages\n",
    "First,make sure that you have installed the required python packages with proper versions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65bb93d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "! python -m pip install towhee gradio"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f4c6d65",
   "metadata": {},
   "source": [
    "## Getting Started\n",
    "\n",
    "Just get your image-generation journey started with the following towhee pipeline!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5c3e6b6c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-27T10:54:42.972208300Z",
     "start_time": "2023-09-27T10:10:28.280300Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "vae\\diffusion_pytorch_model.safetensors not found\n"
     ]
    },
    {
     "data": {
      "text/plain": "Loading pipeline components...:   0%|          | 0/6 [00:00<?, ?it/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "f7516557438345528a1cb135bcd9cdec"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "  0%|          | 0/50 [00:00<?, ?it/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "37017548219245559e2bb873fd0e057b"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from towhee import pipe, ops\n",
    "\n",
    "pipe = (\n",
    "    pipe.input('prompt')\n",
    "        .map('prompt', 'image', ops.text2image.stable_diffusion())\n",
    "        .output('image')\n",
    ")\n",
    "\n",
    "image = pipe('an orange cat').get()[0]\n",
    "image.save('an_orange_cat.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab0a6049",
   "metadata": {},
   "source": [
    "- input(xx):a node in which you can type the text prompt\n",
    "- map():a node loads pretrained diffuser model and generates images\n",
    "- output():a node where the generated image returns"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43ab5ad37e76db00",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Online demo\n",
    "__Gradio is a Python library that simplifies the process of creating web interfaces for machine learning models.__ It provides an easy-to-use interface where developers can quickly prototype and deploy machine learning models with a web-based user interface.By simply annotating inputs and outputs of a model, Gradio automatically generates a customizable UI, allowing users to interact with the model through a web browser.\n",
    "\n",
    "Now with gradio,you will see the UI where you can put your prompt in and then you are able to get a highly-correlated image.Just have a try at the online demo please!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f00c3ca9ac691b6b",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-27T10:54:51.151711100Z",
     "start_time": "2023-09-27T10:54:43.009292700Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7860\n",
      "Running on public URL: https://5bf2c48a91014f6ff2.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
     ]
    },
    {
     "data": {
      "text/plain": "<IPython.core.display.HTML object>",
      "text/html": "<div><iframe src=\"https://5bf2c48a91014f6ff2.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": ""
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gradio\n",
    "from towhee import pipe\n",
    "\n",
    "interface = gradio.Interface(pipe,\n",
    "                             inputs=gradio.Textbox(label='prompt',info='type anything you want'),\n",
    "                             outputs=gradio.Image(type='numpy')\n",
    "                             )\n",
    "interface.launch(share=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6b4c0b3b6edf923e",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-27T10:54:51.205016500Z",
     "start_time": "2023-09-27T10:54:51.157942800Z"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
